
using Elsa.Common;
using Elsa.EntityFrameworkCore.Extensions;
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.EntityFrameworkCore.Modules.Runtime;
using Elsa.Extensions;
using Elsa.Retention.Extensions;
using Elsa.Retention.Models;
using Elsa.Workflows;
using Elsa.Workflows.Management.Entities;
using Elsa.Workflows.Management.Enums;
using Elsa.Workflows.Management.Models;
using ElsaDemo;
using ElsaDemo.ActivityWorkflowAop;

namespace WorkflowDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            //builder.Services.AddSingleton<WorkflowExecMiddleware>();
            //builder.Services.AddSingleton<ActivityExecMiddleware>();
            builder.Services.AddElsa(module =>
            {
                module.UseJavaScript();
                //module.UseFluentStorageProvider();
                module.UseWorkflowMiddleware();
                module.UseWorkflowManagement(managementFeature =>
                {
                    //managementFeature.UseEntityusFrameworkCore(persistence =>
                    //{
                    //    persistence.UseSqlite("Data Source=elsa.db");
                    //});
                });
                module.UseWorkflowRuntime(runtimeFeature =>
                {
                    runtimeFeature.UseEntityFrameworkCore(persistence =>
                    {
                        persistence.UseSqlite("Data Source=elsa.db");
                    });
                });
                module.UseRetention(retentionFeature =>
                {
                    retentionFeature.SweepInterval = TimeSpan.FromDays(30);
                    retentionFeature.AddDeletePolicy("Delete all finished workflows", sp =>
                    {
                        var clock = sp.GetRequiredService<ISystemClock>();
                        DateTimeOffset threshold = clock.UtcNow.Subtract(TimeSpan.FromHours(1));

                        return new RetentionWorkflowInstanceFilter()
                        {
                            TimestampFilters =
                            [
                                new TimestampFilter()
                                {
                                    Column = nameof(WorkflowInstance.FinishedAt),
                                    Operator = TimestampFilterOperator.LessThanOrEqual,
                                    Timestamp = threshold
                                }
                            ],
                            WorkflowStatus = WorkflowStatus.Finished
                        };
                    });
                });
                module.AddWorkflowsFrom<Program>();
                module.AddActivitiesFrom<Program>();
            });
            builder.Services.AddHostedService<TestHostedService>();
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();
            app.UseAuthorization();
            app.MapControllers();

            app.Run();
        }
    }
}
